package org.xukai.common.util.bean;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.xukai.common.Constants;

import java.beans.PropertyDescriptor;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Type : com.weiyi.ehr.utils
 * @Author : TimLi
 * @Desc :
 * @Date : 2016-03-17 14:40
 */

public class ToolUtils {

    /**
     * transfer list into map
     * @param list
     * @param fieldName4Key
     * @return map
     *
     * @author sherlock
     * @date 2016-9-16 12:58:53
     */
    public static <K, V> Map<K, V> list2Map(List<V> list, String fieldName4Key,Class<V> c) {
        Map<K, V> map = new HashMap<K, V>();
        if (list != null) {
            try {
                PropertyDescriptor propDesc = new PropertyDescriptor(fieldName4Key, c);
                Method methodGetKey = propDesc.getReadMethod();
                for (int i = 0; i < list.size(); i++) {
                    V value = list.get(i);
                    @SuppressWarnings("unchecked")
                    K key = (K) methodGetKey.invoke(list.get(i));
                    map.put(key, value);
                }
            } catch (Exception e) {
                throw new IllegalArgumentException("field can't match the key!");
            }
        }

        return map;
    }

    /**
     * object To Map
     * @param object
     * @return
     */
    public static Map<String,String> objectToMap(Object object){
        try {
          return   BeanUtils.describe(object);
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * List<Object>  To List<Map<String,String>
     * @param lists
     * @return
     */
    public static List<Map<String,String>> listToListMap(List<Object> lists){
        List<Map<String,String>> listMap=new ArrayList<Map<String,String>>();
        for(Object object:lists){
            listMap.add(objectToMap(object));
        }
        return listMap;
    }
    /**
     * 获取医院Id和子项Id，格式 医院ID:子项ID;医院ID:子项ID
     * @param allId
     * @return
     */
    public static Map<String,String> getServiceCode(String allId){
        Map<String, String> serviceMap = new HashMap<String, String>();
        if(StringUtils.isBlank(allId)){
            return serviceMap;
        }
        String[] strIdList = allId.split(";");
        if (strIdList.length > 0) {
            for (String str : strIdList) {
                String[] temp = str.split(":");
                if (temp.length == 2) {
                    if (serviceMap.containsKey(temp[0])) {
                        StringBuffer buffer = new StringBuffer(serviceMap.get(temp[0])).append(",").append(temp[1]);
                        serviceMap.put(temp[0], buffer.toString());
                    } else {
                        serviceMap.put(temp[0], temp[1]);
                    }
                }
            }
        }
        return serviceMap;
    }


    public static void objectToObject(Object dest, Object org) {
        try {
              BeanUtils.copyProperties(dest,org);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }


    /**
     *
     * @param centerlist
     * @param hosplist
     * @return    1 正常，2 医院流程异常，3 中心库流程异常 0 未知异常
     */
    public static int listIsEqualSize(int centerlist, int hosplist) {

        if(centerlist==0&&hosplist==0){
            return 1;
        }
        if(centerlist!=0&&hosplist==0){
            return 2;
        }
        if(centerlist==0&&hosplist!=0){
            return 3;
        }
        return 0;
    }


    public static int getNumByString(String val) {
        int num = 0;
        try {
            if (!org.springframework.util.StringUtils.isEmpty(val)) {
                num = Integer.parseInt(val.trim());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return num;
    }

    public static  String getValueByTrim(String val) {
        if (org.springframework.util.StringUtils.isEmpty(val) || "null".equalsIgnoreCase(val)) {
            return "";
        }
        return val.trim();
    }

    public static String messyCode(String path) throws UnsupportedEncodingException {
          String encoding = System.getProperty("file.encoding");
        return new String(path.getBytes(Constants.CHARSET_UTF8),encoding);
    }
}
